package com.poker.controller;

import com.poker.entity.Activity;
import com.poker.entity.ActivitySchedule;
import com.poker.service.ActivityScheduleService;
import com.poker.service.ActivityService;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName 活动日程
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/19 16:32
 * @Version 1.0
 **/
@Controller
@RequestMapping("/activitySchedule")
public class ActivityScheduleController extends BaseController {

    @Resource
    private ActivityScheduleService activityScheduleService;

    @Resource
    private ActivityService activityService;

    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "50") Integer size){
        Integer managerInfo = getManagerInfo();
        Page<ActivitySchedule> data = activityScheduleService.findList(page,size,managerInfo);
        model.addAttribute("activitySchedule", new ActivitySchedule());
        model.addAttribute("data", data);
        return managerPage("activitySchedule/list");
    }

    @RequestMapping("/findQuery")
    public String findQuery(Model model,ActivitySchedule activitySchedule,
                            @RequestParam(value = "page", defaultValue = "0") Integer page,
                            @RequestParam(value = "size", defaultValue = "50") Integer size){
        Integer managerInfo = getManagerInfo();
        Page<ActivitySchedule> data = activityScheduleService.findListAll(page,size,activitySchedule,managerInfo);
        model.addAttribute("activitySchedule",activitySchedule);
        model.addAttribute("data", data);
        return managerPage("activitySchedule/list");

    }

    @RequestMapping("/edit")
    public String edit(Model model,String id){
        Activity activity = activityService.getById(id);
        if(activity!=null){
            ActivitySchedule schedule=new ActivitySchedule();
            schedule.setActivityId(activity.getId());
            schedule.setActivityName(activity.getActivityName());
            model.addAttribute("ActivitySchedule", schedule);
            return managerPage("activitySchedule/edit");
        }
        return toJson(true,"参数异常！",null);
    }

    @ResponseBody
    @RequestMapping("/add")
    public String add(ActivitySchedule activitySchedule){
        activityScheduleService.saveActivitySchedule(activitySchedule);
        return toJson(true,"success",null);
    }

    @RequestMapping("/delete")
    public String delete(String id, Integer page) {
        if (Misc.isEmpty(id)) return toJson(false, "id信息为空", null);

        if (Misc.isEmpty(page)) return toJson(false, "page信息为空", null);

        ActivitySchedule activitySchedule = activityScheduleService.getById(id);
        if (Misc.isEmpty(activitySchedule)) return toJson(false, "目标信息不存在", null);

        activityScheduleService.deleteactivitySchedule(activitySchedule);

        return redirect("/activitySchedule/list");
    }
}
